import { defHttp } from '../utils/http/axios';
import { ErrorMessageMode } from '../utils/http/axios';
import { withLoading } from '../utils/http/http';
import type { LoginParams, LoginResultModel, GetUserInfoModel, SmsLoginParams } from './model/userModel';

enum Api {
  Login = '/admin-api/system/auth/login',
  Logout = '/admin-api/system/auth/logout',
  SmsLogin = '/admin-api/system/auth/sms-login',
  GetUserInfo = '/admin-api/system/auth/get-permission-info',
}

/**
 * @description: 用户登录API
 */
export function loginApi(params: LoginParams, mode: string = 'modal') {
  return withLoading(() =>
    defHttp.post<LoginResultModel>(
      {
        url: Api.Login,
        params,
      },
      {
        errorMessageMode: mode as ErrorMessageMode,
        joinPrefix: false,
      },
    )
  );
}

/**
 * @description: 短信登录API
 */
export function smsLogin(params: SmsLoginParams, mode: string = 'modal') {
  return withLoading(() =>
    defHttp.post<LoginResultModel>(
      {
        url: Api.SmsLogin,
        params,
      },
      {
        errorMessageMode: mode as ErrorMessageMode,
        joinPrefix: false,
      },
    )
  );
}

/**
 * @description: 获取用户信息
 */
export function getUserInfo() {
  return withLoading(() =>
    defHttp.get<GetUserInfoModel>(
      {
        url: Api.GetUserInfo
      },
      {
        errorMessageMode: ErrorMessageMode.NONE,
        joinPrefix: false,
      }
    )
  );
}

/**
 * @description: 用户登出
 */
export function doLogout() {
  return withLoading(() =>
    defHttp.post(
      {
        url: Api.Logout
      },
      {
        joinPrefix: false,
      }
    )
  );
}
